package wowraidmanager.client.wishlist.wishlist;

import java.util.ArrayList;

import wowraidmanager.client.BaseComposite;
import wowraidmanager.client.service.wishlist.MemberService;
import wowraidmanager.client.service.wishlist.MemberServiceAsync;
import wowraidmanager.client.service.wishlist.RaidbossDroplistService;
import wowraidmanager.client.service.wishlist.RaidbossDroplistServiceAsync;
import wowraidmanager.shared.model.wishlist.Droplist;
import wowraidmanager.shared.model.wishlist.Member;
import wowraidmanager.shared.model.wishlist.RaidbossDroplist;
import wowraidmanager.shared.model.wishlist.UserStatus;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.CheckBox;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.HasVerticalAlignment;
import com.google.gwt.user.client.ui.Widget;

public class EditwishlistBossInfo extends BaseComposite{

    private static EditwishlistBossInfoUiBinder uiBinder = GWT.create(EditwishlistBossInfoUiBinder.class);

    protected RaidbossDroplistServiceAsync raidbossDroplistService = GWT.create(RaidbossDroplistService.class);

    protected MemberServiceAsync memberService = GWT.create(MemberService.class);

    interface EditwishlistBossInfoUiBinder extends UiBinder<Widget, EditwishlistBossInfo>{
    }

    String raidzone;

    UserStatus userStatus;

    ArrayList<RaidbossDroplist> raidzoneDroplist;

    Member currentMember;

    @UiField
    FlexTable bossInfo;

    @UiField
    Button setButton;

    public EditwishlistBossInfo(String raidzone, UserStatus userStatus, ArrayList<RaidbossDroplist> raidzoneDroplist, Member currentMember){
        initWidget(uiBinder.createAndBindUi(this));
        this.raidzone = raidzone;
        this.userStatus = userStatus;
        this.raidzoneDroplist = raidzoneDroplist;
        this.currentMember = currentMember;

        setBossInfoList();
        setButton();
    }

    private void setButton(){
        setButton.addClickHandler(new ClickHandler(){

            public void onClick(ClickEvent event){
                setCurrentMember();
            }
        });

    }

    protected void setCurrentMember(){
        memberService.getMember(userStatus.getMember().getMemberAccount(), new AsyncCallback<ArrayList<Member>>(){

            public void onSuccess(ArrayList<Member> result){
                for(Member member: result){
                    if(member.getKey().equals(currentMember.getKey())){
                        currentMember = member;
                        break;
                    }
                }
                setWishlist();
            }

            public void onFailure(Throwable caught){
                clearInfoMessage();
                setErrorMessage("更新に失敗しました。");
            }
        });

    }

    protected void setWishlist(){
        ArrayList<Droplist> currentwishlist = currentMember.getWishlist();

        ArrayList<Droplist> removedroplist = new ArrayList<Droplist>();
        if(currentwishlist != null){
            for(Droplist droplist:currentwishlist){
                if(droplist.getRaidzone().equals(raidzone)){
                    removedroplist.add(droplist);
                }
            }
            currentwishlist.removeAll(removedroplist);
        }else{
            currentwishlist = new ArrayList<Droplist>();
        }

        int column = 0;
        for(RaidbossDroplist droplist:raidzoneDroplist){
            FlexTable droplistTable = (FlexTable)bossInfo.getWidget(0, column);

            for(int row = 2;row < droplistTable.getRowCount();row++){
                if(droplistTable.getFlexCellFormatter().getColSpan(row, 0) == 1){
                    CheckBox itemCheckBox = (CheckBox)droplistTable.getWidget(row, 0);
                    if(itemCheckBox.getValue()){
                        String itemId = itemCheckBox.getFormValue();

                        for(Droplist item:droplist.getDroplist()){
                            if(item.getId().equals(itemId)){
                                if(!currentwishlist.contains(item)){
                                    currentwishlist.add(item);
                                }
                            }
                        }
                    }
                }
            }
            column++;
        }

        currentMember.setWishlist(currentwishlist);

        memberService.setMember(currentMember, new AsyncCallback<Void>(){

            public void onSuccess(Void result){
                setInfoMessage(raidzone + "のWishlistの更新に成功しました。");
                clearErrorMessage();
            }

            public void onFailure(Throwable caught){
                clearInfoMessage();
                setErrorMessage(raidzone + "のWishlistの更新に失敗しました。");

            }
        });
    }

    protected void setBossInfoList(){
        int column = 0;
        ArrayList<Droplist> currentWishlist = currentMember.getWishlist();
        for(RaidbossDroplist droplist:raidzoneDroplist){
            int droplistrow = 0;
            FlexTable droplistTable = new FlexTable();
            droplistTable.setBorderWidth(1);
            droplistTable.setStyleName("flexTable");

            droplistTable.setHTML(droplistrow, 0, "<a href=\"http://www.wowhead.com/npc=" + droplist.getBossId() + "\" target=\"_blank\">" + droplist.getBossName() + "</a>");
            droplistTable.getFlexCellFormatter().setColSpan(droplistrow, 0, 2);
            droplistrow++;
            droplistTable.setText(droplistrow, 0, "Normal");
            droplistTable.getFlexCellFormatter().setColSpan(droplistrow, 0, 2);
            droplistTable.getCellFormatter().setStyleName(droplistrow, 0, "table-header");
            droplistrow++;

            for(Droplist item:droplist.getDroplist()){
                if(!item.isHeroic()){
                    CheckBox itemCheckBox = new CheckBox();
                    itemCheckBox.setFormValue(item.getId());
                    if(currentWishlist != null){
                        for(Droplist wishlistItem:currentWishlist){
                            if(wishlistItem.getId().equals(itemCheckBox.getFormValue()) && wishlistItem.getBossName().equals(item.getBossName())){
                                itemCheckBox.setValue(true);
                            }
                        }
                    }

                    droplistTable.setWidget(droplistrow, 0, itemCheckBox);
                    droplistTable.getCellFormatter().setHorizontalAlignment(droplistrow, 0, HasHorizontalAlignment.ALIGN_CENTER);
                    droplistTable.setHTML(droplistrow, 1, "<a href=\"http://www.wowhead.com/item=" + item.getId() + "\" rel=\"item=" + item.getId() + "\" class=\"q" + (Integer.valueOf(item.getItemColor()) + 1) + "\" target=\"_blank\">[" + item.getItemName() + "]</a>");
                    droplistTable.getCellFormatter().setStyleName(droplistrow, 1, "itemlink");
                    droplistrow++;
                }
            }

            droplistTable.setText(droplistrow, 0, "Heroic");
            droplistTable.getFlexCellFormatter().setColSpan(droplistrow, 0, 2);
            droplistTable.getCellFormatter().setStyleName(droplistrow, 0, "table-header");
            droplistrow++;

            for(Droplist item:droplist.getDroplist()){
                if(item.isHeroic()){
                    CheckBox itemCheckBox = new CheckBox();
                    itemCheckBox.setFormValue(item.getId());
                    if(currentWishlist != null){
                        for(Droplist wishlistItem:currentWishlist){
                            if(wishlistItem.getId().equals(itemCheckBox.getFormValue()) && wishlistItem.getBossName().equals(item.getBossName())){
                                itemCheckBox.setValue(true);
                            }
                        }
                    }
                    droplistTable.setWidget(droplistrow, 0, itemCheckBox);
                    droplistTable.getCellFormatter().setHorizontalAlignment(droplistrow, 0, HasHorizontalAlignment.ALIGN_CENTER);
                    droplistTable.setHTML(droplistrow, 1, "<a href=\"http://www.wowhead.com/item=" + item.getId() + "\" rel=\"item=" + item.getId() + "\" class=\"q" + (Integer.valueOf(item.getItemColor()) + 1) + "\" target=\"_blank\">[" + item.getItemName() + "]</a>");
                    droplistTable.getCellFormatter().setStyleName(droplistrow, 1, "itemlink");
                    droplistrow++;
                }
            }

            bossInfo.setWidget(0, column, droplistTable);
            bossInfo.getCellFormatter().setVerticalAlignment(0, column, HasVerticalAlignment.ALIGN_TOP);

            column++;
        }

    }
}
